import { defineStore } from 'pinia'
export const useStore = defineStore('main', {
    state() {
        return {
            userInfo: uni.getStorageSync('userInfo')
                ? JSON.parse(uni.getStorageSync('userInfo'))
                : '',
            orgType: uni.getStorageSync('orgType') || '',
            token: uni.getStorageSync('token') || '',
        }
    },
    getters: {
        userId() {
            return this.userInfo?.id
        },
        username() {
            return this.userInfo?.name
        },
        token() {
            return this.token
        },
    },
    actions: {
        async clear() {
            this.userInfo = {}
        },
        // async getUserInfoByCode(code) {
        //   try {
        //     const res = await http.post('/wxUser/getOpenid', { code })
        //     let userInfo = { ...res.data, ...res.data.attach, unionid: res.data.wxUser.unionid, nickname: res.data.wxUser.nickname, headimgurl: res.data.wxUser.headimgurl }
        //     uni.setStorageSync('openid', userInfo.openId)
        //     uni.setStorageSync('unionid', userInfo.unionid)
        //     delete userInfo.attach
        //     delete userInfo.wxUser
        //     this.userInfo = userInfo
        //     uni.setStorageSync('userInfo', JSON.stringify(this.userInfo))
        //     return Promise.resolve(res.data)
        //   } catch (error) {
        //     return Promise.reject(error)
        //   }
        // },
        setParamsObj(url, obj) {
            if (!obj) {
                uni.removeStorageSync(url)
            }
            uni.setStorageSync(url, JSON.stringify(obj))
            window.history.pushState(null, null, document.URL + '#1')
            uni.switchTab({ url: url })
        },
    },
})

// export const store = useStore()
